package com.tcl.launcherpro.lockscreen;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.tcl.launcherpro.lockscreen.interfaces.ILauncherSettingListener;

/**
 * <br>类描述:管理锁屏相关功能
 * <br>详细描述:用于初始化以及回收锁屏功能所用的资源
 * <br><b>Author sichard</b>
 * <br><b>Date 2017/2/15</b>
 */

public class LockScreenManager {
    private static LockScreenManager sInstance;
    private Context mContext;
    private LockScreenReceiver mLockScreenReceiver;
    private ILauncherSettingListener mLauncherSettingListener;

    public static LockScreenManager getInstance(Context context){
        if (sInstance == null) {
            sInstance = new LockScreenManager(context);
        }
        return sInstance;
    }

    private LockScreenManager(Context context) {
        this.mContext = context.getApplicationContext();
        init();
    }

    private void init() {
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        mLockScreenReceiver = new LockScreenReceiver();
        mContext.registerReceiver(mLockScreenReceiver, filter);
    }

    public void setLauncherSettingListener(ILauncherSettingListener launcherSettingListener) {
        this.mLauncherSettingListener = launcherSettingListener;
    }

    /**
     * 向服务器发出请求，以回调的方式返回是否显示充电屏保的标志位
     */
    public void requestLauncherSetting() {
        if (mLauncherSettingListener != null) {
            mLauncherSettingListener.request();
        }
    }

    /**
     * 获取用户在桌面设置界面显示充电屏保
     * @return true,显示；false,隐藏
     */
    public boolean isShowLockScreen() {
        if (mLauncherSettingListener != null) {
            return mLauncherSettingListener.isShowLockScreen();
        }
        return true;
    }

    /**
     * 获取服务器是否显示充电屏保
     * @return true,显示；false,隐藏
     */
    public boolean isShowLockScreenFromServer() {
        if (mLauncherSettingListener != null) {
            return mLauncherSettingListener.isShowLockScreenFromServer();
        }
        return true;
    }


    public void release() {
        mContext.unregisterReceiver(mLockScreenReceiver);
        mContext = null;
        sInstance = null;
    }
}
